package com.sgcc.fj.pmos.e_fj_personalcenter.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.arouter.personalcenter.PersonalCenterArouterPath;
import com.sgcc.fj.pmos.common.base.BaseActivity;
import com.sgcc.fj.pmos.common.bean.BaseResponse;
import com.sgcc.fj.pmos.common.bean.EventCenter;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvcNetCallback;
import com.sgcc.fj.pmos.common.utils.ArguConstant;
import com.sgcc.fj.pmos.common.widget.MyEditText;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.e_fj_personalcenter.bean.PowerCurveBean;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 新增曲线
 */
@Route(path = PersonalCenterArouterPath.PERSONALCENTER_NEWCURVE)
public class NewCurveActivity extends BaseActivity {

    private TitleBar mTitleBar;
    private MyEditText mEtCurvename;
    private MyEditText mEtPerson;
    private MyEditText mEtNote;
    private RelativeLayout mRlSavecurve;
    private TextView mTvBottom;

    @Autowired
    public String from;

    @Autowired
    public PowerCurveBean.DataBean mBean;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_newcurve;
    }

    @Override
    protected void initViews() {
        super.initViews();

        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mEtCurvename = (MyEditText) findViewById(R.id.et_curvename);
        mEtPerson = (MyEditText) findViewById(R.id.et_person);
        mEtNote = (MyEditText) findViewById(R.id.et_note);
        mRlSavecurve = (RelativeLayout) findViewById(R.id.rl_savecurve);
        mTvBottom = (TextView) findViewById(R.id.tv_bottom);

        mTitleBar.setTitle("1".equals(from) ? "新增用电曲线" : "新增偏差曲线");
        mTvBottom.setText("1".equals(from) ? "保存并设置电量比例" : "保存并设置偏差比例");
        mTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });

        if (mBean != null) {
            mEtCurvename.setText(mBean.getRatioName());
            mEtPerson.setText(mBean.getConfigurePerson());
            mEtNote.setText(mBean.getRemark());
        }

        mRlSavecurve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String curveName = mEtCurvename.getText().toString().trim();
                if (TextUtils.isEmpty(curveName)) {
                    ToastUtils.showShort("请输入曲线名称");
                    return;
                }
                String person = mEtPerson.getText().toString().trim();
                String note = mEtNote.getText().toString().trim();

                String ratioId = null;
                if (mBean != null) {
                    ratioId = mBean.getRatioId();
                }

                saveCurveInfo(curveName, person, note, ratioId);

            }
        });
    }

    public void saveCurveInfo(String ratioName, String configurePerson, String remark, String ratioId) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("ratioId", ratioId);
        params.put("remark", remark);
        params.put("configurePerson", configurePerson);
        params.put("ratioName", ratioName);
        params.put("eSource", "1".equals(from) ? "3" : "4");
        HttpRequestUtils.getInstance().request(params, HttpInterConst.USER_SAVECURVEINFO, new MvcNetCallback<BaseResponse>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁

            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (response != null) {
                    if (response.isSuccess()) {
                        if (mBean != null) {
                            EventBus.getDefault().post(new EventCenter<>(ArguConstant.SAVE_CURVE_BACK));
                            finish();
                        } else {
                            queryCurveInfo(ratioName);
                        }
                    } else {
                        if (!TextUtils.isEmpty(response.getMsg())) {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }
            }
        });
    }

    public void queryCurveInfo(String ratioName) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("ratioId", "");
        params.put("eSource", "1".equals(from) ? "3" : "4");
        HttpRequestUtils.getInstance().request(params, HttpInterConst.USER_QUERYCURVEINFO, new MvcNetCallback<PowerCurveBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);

            }

            @Override
            public void onResponse(PowerCurveBean response, int id) {
                super.onResponse(response, id);
                if (response != null) {
                    if ("200".equals(response.getCode())) {
                        if (!response.getData().isEmpty()) {
                            PowerCurveBean.DataBean bean = getBean(ratioName, response.getData());
                            if (bean != null) {
                                ARouter.getInstance()
                                        .build(PersonalCenterArouterPath.PERSONALCENTER_SETPOWERRATIO)
                                        .withString("ratioId", bean.getRatioId())
                                        .withString("from", from)
                                        .withBoolean("isEdit", mBean != null)
                                        .navigation();
                                finish();
                            }
                        }
                    }
                }
            }
        });
    }

    public PowerCurveBean.DataBean getBean(String ratioName, List<PowerCurveBean.DataBean> list) {
        for (PowerCurveBean.DataBean bean : list) {
            if (ratioName.equals(bean.getRatioName())) {
                return bean;
            }
        }
        return null;
    }

}
